package com.icen.utils.comm;

import java.util.Random;

/**
 * Random Utils Class
 * 
 * @author IcenHan
 *
 */
public class RandomUtils {
	public static final String NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUMBERS = "0123456789";
	public static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";

	/**
	 * 获取随机的数字和字母
	 * 
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandomNumbersAndLetters(int length) {
		return getRandom(NUMBERS_AND_LETTERS, length);
	}

	/**
	 * 获取随机的数字
	 * 
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandomNumbers(int length) {
		return getRandom(NUMBERS, length);
	}

	/**
	 * 获取随机的字母
	 * 
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandomLetters(int length) {
		return getRandom(LETTERS, length);
	}

	/**
	 * 获取随机的大写字母
	 * 
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandomCapitalLetters(int length) {
		return getRandom(CAPITAL_LETTERS, length);
	}

	/**
	 * 获取随机的小写字母
	 * 
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandomLowerCaseLetters(int length) {
		return getRandom(LOWER_CASE_LETTERS, length);
	}

	/**
	 * 在一段字符串中获取随机值
	 * 
	 * @param str
	 *            字符串
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandom(String str, int length) {
		return ValidateUtils.isEmpty(str) ? null : getRandom(
				str.toCharArray(), length);
	}

	/**
	 * 在一个数组中获取随机值
	 * 
	 * @param sourceChar
	 *            数据
	 * @param length
	 *            长度
	 * @return String
	 */
	public static String getRandom(char[] sourceChar, int length) {
		if (sourceChar == null || sourceChar.length == 0 || length < 0) {
			return null;
		}

		StringBuilder str = new StringBuilder(length);
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			str.append(sourceChar[random.nextInt(sourceChar.length)]);
		}
		return str.toString();
	}

	/**
	 * 获取一段自然数中的随机值,从零开始
	 * 
	 * @param maxNum
	 *            最大值
	 * @return int
	 */
	public static int getRandom(int maxNum) {
		return getRandom(0, maxNum);
	}

	/**
	 * 获取一段自然数中的随机值
	 * 
	 * @param minNum
	 *            开始值
	 * @param maxNum
	 *            结束值
	 * @return int
	 */
	public static int getRandom(int minNum, int maxNum) {
		if (minNum > maxNum) {
			return 0;
		}
		if (minNum == maxNum) {
			return minNum;
		}
		return minNum + new Random().nextInt(maxNum - minNum);
	}

	/**
	 * 洗牌一组数组
	 * 
	 * @param objArr
	 * @return boolean
	 */
	public static boolean shuffle(Object[] objArr) {
		if (objArr == null) {
			return false;
		}

		return shuffle(objArr, getRandom(objArr.length));
	}

	/**
	 * 洗牌,数组重新排序
	 * 
	 * @param objArr
	 * @param shuffleCount
	 *            洗牌次数
	 * @return boolean
	 */
	public static boolean shuffle(Object[] objArr, int shuffleCount) {
		int length;
		if (objArr == null || shuffleCount < 0
				|| (length = objArr.length) < shuffleCount) {
			return false;
		}

		for (int i = 1; i <= shuffleCount; i++) {
			int random = getRandom(length - i);
			Object temp = objArr[length - i];
			objArr[length - i] = objArr[random];
			objArr[random] = temp;
		}
		return true;
	}

	/**
	 * 洗牌一组数组
	 * 
	 * @param intArr
	 * @return
	 */
	public static int[] shuffle(int[] intArr) {
		if (intArr == null) {
			return null;
		}

		return shuffle(intArr, getRandom(intArr.length));
	}

	/**
	 * 洗牌,数组重新排序
	 * 
	 * @param intArr
	 * @param shuffleCount
	 *            洗牌次数
	 * @return boolean
	 */
	public static int[] shuffle(int[] intArr, int shuffleCount) {
		int length;
		if (intArr == null || shuffleCount < 0
				|| (length = intArr.length) < shuffleCount) {
			return null;
		}

		int[] out = new int[shuffleCount];
		for (int i = 1; i <= shuffleCount; i++) {
			int random = getRandom(length - i);
			out[i - 1] = intArr[random];
			int temp = intArr[length - i];
			intArr[length - i] = intArr[random];
			intArr[random] = temp;
		}
		return out;
	}
}
